#!/usr/bin/env python3
import time
import cv2

vc = cv2.VideoCapture(0)
face_cascade = cv2.CascadeClassifier('face1.xml')
eye_cascade = cv2.CascadeClassifier('eye.xml')

if vc.isOpened(): # try to get the first frame
    rval, frame = vc.read()
    #print('Original Dimensions : ',frame.shape)
else:
    rval = False

#dim = (1280//2,720//2)
while rval:
    #读取摄像头图像
    rval, img = vc.read()
    #设置大小
    #img = cv2.resize(img, dim, interpolation = cv2.INTER_AREA)
    #镜像
    img = cv2.flip(img, 1)

    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)

    for (x,y,w,h) in faces:
        cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = img[y:y+h, x:x+w]
        print (int(x+w/2), int(y+h/2))
        time.sleep(0.5)

        eyes = eye_cascade.detectMultiScale(roi_gray)
        for (ex,ey,ew,eh) in eyes:
            cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)

    cv2.imshow("Resize Preview", img)
    
    #按键退出
    key = cv2.waitKey(20)
    if key == 27: # exit on ESC
        break
cv2.destroyWindow("Resize Preview")
